﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; 
using Runda.CMS.Bll;
using Runda.CMS.Common.Utility;
using System.Data;
using Runda.CMS.Order.Entity;
using Runda.CMS.Order.Repository;

namespace Runda.CMS.Portal.Customer
{
    public partial class CustomerAccount : BasePage
    {
        private AccountRepository accountRepository = new AccountRepository();

        public String customerID
        {
            get
            {
                if (ViewState["customerID"] == null) return String.Empty;
                else return ViewState["customerID"].ToString();
            }
            set { ViewState["customerID"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {              
                if (Request.QueryString["customerID"] != null)
                {
                    customerID =  Request.QueryString["customerID"];
                    SetFormData();
                }
            }
        }
     
        private void SetFormData()
        {
            if (customerID != String.Empty)
            {
                Account account = accountRepository.findAccountByCustomerID(customerID);
                if (account != null) {
                    AccountId.Text = account.AccountID.ToString();
                    txtAccount.Text = account.accountName;
                    txtAccount.Enabled = false;
                }
            }
        }      

        protected void lnkbtnSaveAndClose_Click(object sender, EventArgs e)
        {
            string Message = "";
            if (!Save(ref Message))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "saveandssscloseerror", "alert('" + Message + "');", true);
            }
            else
                ClientScript.RegisterStartupScript(this.GetType(), "saveandclose", "window.close();", true);
        }

       

        private bool Save(ref string Message)
        {
            Account account = null;
            String password = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(txtPassword.Text.Trim(), "MD5");
            if (AccountId.Text.Trim() == "")
            {
                account = accountRepository.findAccountByAccountName(txtAccount.Text.Trim());

                if (account != null)
                {
                    Message = "您输入的帐号已存在";
                    return false;
                }            
                account = new Account(password,customerID,txtAccount.Text.Trim());
            }else{                
                account = accountRepository.findAccountById(new Guid(AccountId.Text));
                account.resetPassword(password);
            }
            if (AccountId.Text != "")
            {
                accountRepository.merge(account);
            }
            else {
                accountRepository.persist(account);
            }
 
            return true;
        } 
    }
}
